use global_hotkey::{GlobalHotKeyEvent, HotKeyState};

use crate::shortcut::shortcut::DEFAULT_ACTIVE_WINDOW_KEY;
use crate::state;
use crate::utils::window;

/// 默认激活窗口快捷键
pub fn handle(event: GlobalHotKeyEvent) {
    // 不处理按下事件
    if HotKeyState::Pressed == event.state() {
        return;
    }

    if DEFAULT_ACTIVE_WINDOW_KEY.id() == event.id() {
        let window = state::application::application().window();
        if window.is_some() {
            let _ = window.unwrap().set_focus();

            return;
        }

        // 保留全局主窗口
        let window = window::build_main_window();
        if window.is_err() {
            log::error!("构建主窗口失败：{}", window.err().unwrap());

            return;
        }

        state::application::application_mut().set_window(Some(window.unwrap()));
    }
}